<?


class objStorage {
	public $id = 0;
	public $index = 0;
	public $type = '';
	public $fsType = 'Unknown';
	public $description = '';
	public $units = 'bytes';
	public $blockSize = 1024;
	public $size = 0;
	public function __construct($cfg=array()) {
		if (is_array($cfg) || is_object($cfg)) {
			
			foreach ($cfg AS $name=>$value) {
				switch ($name) {
					case 'hrStorageIndex':
						$this->index = $value;
						break;
					case 'hrStorageType':
						$this->type = $value;
						break;
					case 'hrStorageAllocationUnits':
						$parts = explode(' ',$value);
						switch ($parts[1]) {
							case 'Bytes':
								$this->units = strtolower($parts[1]);
								break;	
							case 'KBytes':
								$this->units = strtolower($parts[1])*1024;
								break;				
						}
						$this->blockSize = $parts[0];
						break;
					case 'hrStorageDescr':
						$this->description = $value;
						break;
					case 'hrStorageSize':
						$this->size = $value;
						break;
					case 'hrStorageUsed':
						$this->used = $value;
						break;
					case 'hrFSType':
						switch ($value) {
							case 'hrFSNFS'; $this->fsType = 'NFS'; break;
							case 'hrFSLinuxExt2': $this->fsType = 'Ext2'; break;
							case 'hrFSNTFS': $this->fsType = 'NTFS'; break;
							case 'hrFSFat': $this->fsType = 'FAT'; break;
							default:
								$this->fsType = 'Other';
						}
						break;

				
				}
			
			
			}		
		}	
	}
	
	
	public function getDescription() { return $this->description;}
	public function getPercentageLoad() { 
		if ($this->size>0) {
			return round(
				(
					($this->used*$this->blockSize)/($this->size*$this->blockSize))*100
			,0);
		} else return 0;
	}
	
	public function getSize($format=true) {
		if ($format==true) {
			return $this->formatBytes($this->size*$this->blockSize);
		} else {
			return $this->size*$this->blockSize;
		}
	}
	public function getUsed($format=true) {
		if ($format==true) {
			return $this->formatBytes($this->used*$this->blockSize);
		} else {
			return $this->used*$this->blockSize;
		}
	}
	
	private function formatBytes($bytes, $precision = 2) { 
 		$units = array('B', 'KB', 'MB', 'GB', 'TB'); 
		
		$bytes = max($bytes, 0); 
		$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
		$pow = min($pow, count($units) - 1); 
		
		$bytes /= pow(1024, $pow); 
		
		return round($bytes, $precision) . ' ' . $units[$pow]; 
	}
	
	public function storeUsageRrd($path,$step=60) {
		if (!is_file($path)) {
			$opts = array(
				'--step',$step,
				'DS:size:GAUGE:600:0:U',
				'DS:used:GAUGE:600:0:U',
				'DS:perc:GAUGE:600:0:U',
				'RRA:AVERAGE:0.5:1:800',
				'RRA:AVERAGE:0.5:6:800',
				'RRA:AVERAGE:0.5:24:800',
				'RRA:AVERAGE:0.5:288:800',
				'RRA:MAX:0.5:1:800',
				'RRA:MAX:0.5:6:800',
				'RRA:MAX:0.5:24:800',
				'RRA:MAX:0.5:288:800'
			);
			if (rrd_create($path, $opts, count($opts)) == 0) { 
				echo 'Error: '.rrd_error().chr(10);
				// Log Error
				return false;
			}
		}		
		$rrd = array();
		$rrd[] = 'N';
		$rrd[] = $this->getSize(false);
		$rrd[] = $this->getUsed(false);
		$rrd[] = $this->getPercentageLoad(false);
		echo "Storage (".$this->index."): ".implode(':',$rrd).chr(10);
		if (rrd_update($path,implode(':',$rrd)) == 0) {
			echo rrd_error();
			return false;
		} else {
			return true;
		}
	}

	
 }
?>